package com.example.olamundo;

import java.util.Random;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private final Random gerador = new Random();
	private int valor;
	private int pontos;
	private Button btnGuess;
	private Button btnRecomeca;
	private TextView txtPontos;
	private EditText txtguess;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		btnGuess = (Button)findViewById(R.id.button1);
		btnRecomeca = (Button)findViewById(R.id.button2);
		txtPontos = (TextView)findViewById(R.id.textView2);
		txtguess = (EditText)findViewById(R.id.editText1);
		novoValor();
		
		btnGuess.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(txtguess.getText() == null || txtguess.getText().toString().trim().isEmpty()){
					return;
				}
				
				int guess = Integer.parseInt(txtguess.getText().toString());
				
				if(guess == valor){
					atualizarPontos();
					mostraPontos();
					Toast.makeText(getApplicationContext(), "Acertou!", Toast.LENGTH_SHORT).show();
				}else if(guess < valor){
					Toast.makeText(getApplicationContext(), "Para cima...", Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(getApplicationContext(), "Para baixo...", Toast.LENGTH_SHORT).show();
				}
				
				txtguess.setText("");
				
			}
		});
		
		btnRecomeca.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				String temp = String.valueOf(valor);
				Toast.makeText(getApplicationContext(), "o numero era: "+temp, Toast.LENGTH_SHORT).show();
				
				novoValor();
				
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	private int geraValorAleatorio(int min, int max){
		int valor = gerador.nextInt(max - min + 1);
		valor = min + valor;
		return valor;
	}
	
	private void novoValor(){
		valor = geraValorAleatorio(1, 10);
	}
	
	private void atualizarPontos(){
		pontos = pontos + 1;
	}
	
	private void mostraPontos(){
		String temp = String.valueOf(pontos);
		txtPontos.setText(temp);
	}

}
